/*****************************************************************************
Copyright 2006 Kervin L. Pierre
    EMAIL: otlkcon@adevsoft.com

This file is part of Otlkcon.

Otlkcon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Otlkcon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with Otlkcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*******************************************************************************/

#pragma once

#include <string>
#include <list>
#include <vector>
#include <map>

using namespace std;

#import <msxml4.dll> raw_interfaces_only
using namespace MSXML2;

#include <protocols/ical/libical/ical.h>
#include <otlkcon_calendar_component.h>

class CaldavConnection
{
    public:
//        MSXML2::IXMLHTTPRequestPtr pXMLHttpReq;

        // Current working values
        wstring uri;
        wstring user;
        wstring pass;

        // The connection ID
        wstring id;

        // Poll interval
        int pollSecs;

        // We're stateless, but still
        int loggedOn;

        CaldavConnection();
		~CaldavConnection();
        HRESULT Init( wstring idStr );
        HRESULT Logon(wstring uri, wstring user, wstring pass );
        HRESULT PutNewComponent( icalcomponent *comp, wchar_t* lpszCalID,
                       wchar_t** lpszCalEtag, ULONG *ulCalETag, ULONG ulFlags);
        wstring ResourcenameFromUrl( wstring url );
        int StatusString2Int( wstring stat );
        HRESULT ReportResponseXML2Components( MSXML2::IXMLDOMDocument2Ptr response, 
                                     otlkcon_calendar_component_array **comps);
        HRESULT GetEventsByName( list<wstring> names,
                           otlkcon_calendar_component_array **comps);
        HRESULT GetEventsByRange( 
                                 struct icaltimetype tm_start,
                                 struct icaltimetype tm_end,
                                 otlkcon_calendar_component_array **comps );
        HRESULT GetCollectionLock( );
        HRESULT ReleaseCollectionLock( );
        HRESULT GetAllEventNames( map<wstring,wstring> *namePairs );
        HRESULT DeleteResource( wstring name );
		HRESULT WaitForResult(MSXML2::IXMLHTTPRequestPtr pRequest);
		void EscapeText(wstring &strText);
		void UnEscapeText(wstring &strText);
        HRESULT ValidateFilename( wstring name );

protected:
	CRITICAL_SECTION	objectCS;
};
